package com.mg.framework.tiled;

import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;

public class Map implements Iterable<MapLayer> {

	public static final int ORIENTATION_ISOMETRIC = 1;
	public static final int ORIENTATION_ORTHOGONAL = 2;
	private int orientation;
	private Properties properties;
	private Vector<MapLayer> layers;
	private Vector<TileSet> tileSets;
	private int tileWidth;
	private int tileHeight;
	private Rectangle bounds;
	private Bitmap mapBitmap;
	private Canvas canvas;

	public int getMapWidth() {
		return bounds.width * tileWidth;
	}

	public int getMapHeight() {
		return bounds.height * tileHeight;
	}

	public Map(int mapWidth, int mapHeight) {
		this.bounds = new Rectangle(mapWidth, mapHeight);
		this.properties = new Properties();
		this.tileSets = new Vector<TileSet>();
		this.layers = new Vector<MapLayer>();
	}

	public void setTileWidth(int tileWidth) {
		this.tileWidth = tileWidth;
	}

	public void setTileHeight(int tileHeight) {
		this.tileHeight = tileHeight;
	}

	public Properties getProperties() {
		return properties;
	}

	public MapLayer addLayer(MapLayer layer) {
		layer.setMap(this);
		layers.add(layer);
		return layer;
	}

	public int getWidth() {
		return bounds.width;
	}

	public int getHeight() {
		return bounds.height;
	}

	public TileSet findTileSetForTileGID(int gid) {
		TileSet has = null;
		for (TileSet tileset : tileSets) {
			if (tileset.getFirstGid() <= gid) {
				has = tileset;
			}
		}
		return has;
	}

	public void setOrientation(int orientation) {
		this.orientation = orientation;
	}

	@Override
	public Iterator<MapLayer> iterator() {
		return layers.iterator();
	}

	public void addTileset(TileSet tileset) {
		if (tileset == null || tileSets.indexOf(tileset) > -1) {
			return;
		}

		tileSets.add(tileset);
	}

	public int getTileWidth() {
		return tileWidth;
	}

	public int getTileHeight() {
		return tileHeight;
	}

	public MapObject getMapObject(String objectGroupName, String mapObjectName) {
		return getObjectGroupByTileLayerName(objectGroupName).getMapObject(
				mapObjectName);
	}

	public Bitmap getMapBitmap() {

		if (mapBitmap == null) {
			this.mapBitmap = Bitmap.createBitmap(bounds.width * getTileWidth(),
					bounds.height * getTileHeight(), Bitmap.Config.ARGB_8888);
			this.canvas = new Canvas(this.mapBitmap);
		} else {
			this.canvas.drawColor(Color.BLACK);
		}

		// Draw each tile map layer
		for (MapLayer layer : this) {
			if (layer instanceof TileLayer) {
				TileLayer tl = (TileLayer) layer;
				canvas.drawBitmap(tl.getMapLayerBitmap(), tl.bounds.x,
						tl.bounds.y, null);
			}
		}

		return this.mapBitmap;
	}

	private ObjectGroup getObjectGroupByTileLayerName(String mapLayerName) {
		for (MapLayer layer : this) {
			if (layer instanceof ObjectGroup && layer.isMe(mapLayerName)) {
				return (ObjectGroup) layer;
			}
		}
		return null;
	}

	public TileLayer getTileLayerByTileLayerName(String mapLayerName) {
		for (MapLayer layer : this) {
			if (layer instanceof TileLayer && layer.isMe(mapLayerName)) {
				return (TileLayer) layer;
			}
		}
		return null;
	}
}
